<?php

class ConfessionController extends Controller {

    public function actionIndex() {
        $this->layout = '//layouts/column2'; 
        $this->render('index');
    }

    /**
     * @return array action filters
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('index', 'recall'),
                'users' => array('*'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function actionRecall() {
        if (!YII_DEBUG && !Yii::app()->request->isAjaxRequest) {
            throw new CHttpException('403', 'Forbidden access.');
        }
        if (empty($_POST['startpoint'])) {
            throw new CHttpException('404', 'Missing "startpoint" POST parameter.');
        }
        $startpoint = $_POST['startpoint'];
        $startpoint = empty($_POST['startpoint']) ? 0 : (int) $_POST['startpoint'];
        $confessions = Confession::model()->getConfessions($startpoint);
        if (count($confessions) == Constant::NUMBER_NEW_PER_PAGE) {
            $startpoint = $startpoint + Constant::NUMBER_NEW_PER_PAGE;
        }
        $this->renderPartial('list', array(
            'confessions' => $confessions,
            'startpoint' => $startpoint
        ));
    }

}